ProjectTree = Ext.extend(Ext.Panel, {
    initComponent: function() {
        //this.title = '';
        this.region = 'center';
        this.layout = 'border';
        this.margins = '0 0 0 0';
        this.border = false;
        this.autoScroll = true;

        this.nfs = false;
        this.curentPath = '/';

        this.prjDetailPanelH = parseInt(getCookie('prjDetailPanelH'));
        this.prjDetailPanelH =(this.prjDetailPanelH) ? this.prjDetailPanelH : 500;

        this.initProjTreePanel(); //this.loadTreeData();
        this.initComonDetail();

        this.items =  [ this.projTreePanel, this.comonDetail ];

        ProjectTree.superclass.initComponent.call(this);

    },
    initProjTreePanel: function() {
        this.projTreePanel = new Ext.ux.tree.TreeGrid({
            title: 'Навигация',
            loadMask: true,
            border: true,
            region: 'west',
            width: 200,
            margins:'0 3 0 0',
            cmargins:'0 3 0 0',
            maskDisabled: false,
            loadMask  : true,
            columns : [
                { header: 'Клиент-Бренд-Акция-Медиа-Контракт', dataIndex: 'text', width: 800, align: 'left', sortable: true }//,
                //{ header: 'Версионность', dataIndex: 'chvf', width: 100, align: 'center', xtype: 'tgcheckcolumn', sortable: false }
            ],
            selModel : new Ext.tree.DefaultSelectionModel({
               listeners:{
                   scope: this,
                   selectionchange: function(sm, node) {
                        if( parseInt(node.id) ) {
                            //this.curentPath = node.getPath();
                            this.setActivDetailPanel(node.attributes);
                        } else {
                            //this.curentPath = '/xnode-24/1';
                            //this.projTreePanel.selectPath(this.curentPath);
                        }
                    }
               }
            }),
            loader: {
                url: 'rest/opr/getObjectsTree',
                listeners: { scope: this,
                    load: function(ct, nd, res) {
                        var ansv = Ext.util.JSON.decode(res.responseText);
                        if( typeof ansv[0] != 'undefined' ) {
                            this.projTreePanel.selectPath(this.curentPath);
                        } else { error_mes('Ошибка',  ansv.error); }
                    }
                }
            }
        });
        
    },
    initComonDetail: function() {
        this.Clients = new AllClientsDetail();
        this.ClientInfo  = new ClientDetail();
        this.BrandInfo    = new BrandDetail();;
        this.ActionInfo   = new ActionDetail();;
        this.MediaInfo    = new MediaDetail();
        this.ContractInfo = new ContractDetail();

        this.comonDetail = new Ext.Panel({
            region: 'center',
            id: 'comonDetail',
            layout: 'card',
            activeItem: 0,
            margins:'0 0 0 0',
            cmargins:'0 0 0 0',
            frame: false,
            border: true,
            html: '---'
//            items: [
//              this.Clients,
//              this.ClientInfo//,
////              this.BrandInfo,
////              this.ActionInfo,
////              this.MediaInfo,
////              this.ContractInfo
//            ]
        });
    },
    initEditWin: function() {
        this.EditWin = new ProjectEditWin();
    },
    addProject: function() { this.EditWin.editRec = {}; this.EditWin.showWin('add'); },
    eddProject: function() { this.EditWin.editRec = this.projectsGrid.getSelectionModel().getSelected(); this.EditWin.showWin('edit'); },
    delProject: function() {
            var rec = this.projectsGrid.getSelectionModel().getSelected();
            if(rec) {
                    Ext.MessageBox.confirm('Внимание !!!', 'Вы действительно хотите удалить эту запись и все связаные с ней  объекты?',
            function(btn) {
                if (btn == 'yes'){
                    var Project_id = rec.data.id;
                    Ext.Ajax.request({
                            url: 'main?action=deleteProject',
                            params: { id: Project_id },
                            scope: this,
                            callback: function(options, success, response) {
                                    var ansv = Ext.decode(response.responseText);
                                    if(ansv.success === true) {
                                            this.projectsStore.load();
                                    } else { error_mes('Ошибка',  ansv.error); }
                            }
                    });
                }
            }, this
        );
            } else error_mes('Внимание!!!', 'Вы не выбрали запись для удаления.');
    },
    setActivDetailPanel: function(data) {
      var level = data.level;
      if(level == 0) {
          Ext.getCmp('comonDetail').layout.setActiveItem('AllClients');
      }
      if(level ==1) {
        Ext.getCmp('comonDetail').layout.setActiveItem('ClientInfo');
      }
      if(level ==2) {
        Ext.getCmp('comonDetail').layout.setActiveItem('BrandInfo');
      }
      if(level ==3) {
        Ext.getCmp('comonDetail').layout.setActiveItem('ActionInfo');
      }
      if(level ==4) {
        this.MediaInfo.getComonInfo(data);
        Ext.getCmp('comonDetail').layout.setActiveItem('MediaInfo');
      }
      if(level ==5) {
        this.ContractInfo.cnt_id = data.object_id;
        this.ContractInfo.getContractDetail();
        this.ContractInfo.setTitle(data.text);
        Ext.getCmp('comonDetail').layout.setActiveItem('ContractInfo');
      }

    }
});
